package com.bolingcavalry.crm.message.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    // 创建交换机（Topic 类型，支持模糊匹配）
    @Bean
    public TopicExchange messageExchange() {
        return new TopicExchange(RabbitMQConstants.EXCHANGE, true, false);
    }

    // 生产技术部队列
    @Bean
    public Queue productionQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_PRODUCTION).build();
    }

    // 综合部队列
    @Bean
    public Queue generalQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_GENERAL).build();
    }

    // 测控部队列
    @Bean
    public Queue mcQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_MC).build();
    }

    // 销售部队列
    @Bean
    public Queue salesQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_SALES).build();
    }

    // 仪表部队列
    @Bean
    public Queue instrumentQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_INSTRUMENT).build();
    }


    // 绑定：exchange + queue + routing key
    @Bean
    public Binding bindingProduction(@Qualifier("productionQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_PRODUCTION);
    }

    @Bean
    public Binding bindingGeneral(@Qualifier("generalQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_GENERAL);
    }

    @Bean
    public Binding bindingMC(@Qualifier("mcQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_MC);
    }

    @Bean
    public Binding bindingSales(@Qualifier("salesQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_SALES);
    }

    @Bean
    public Binding bindingInstrument(@Qualifier("instrumentQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_INSTRUMENT);
    }
    @Bean
    public Queue serviceOrderQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_SERVICE_ORDER).build();
    }

    // 确认单队列
    @Bean
    public Queue confirmationOrderQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_CONFIRMATION_ORDER).build();
    }

    // 任务单队列
    @Bean
    public Queue taskOrderQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_TASK_ORDER).build();
    }

    // 总结单队列
    @Bean
    public Queue summaryOrderQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_SUMMARY_ORDER).build();
    }

    // 服务问题队列
    @Bean
    public Queue serviceProblemQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_SERVICE_PROBLEM).build();
    }

    // 按订单类型区分的绑定
    @Bean
    public Binding bindingServiceOrder(@Qualifier("serviceOrderQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_SERVICE_ORDER);
    }

    @Bean
    public Binding bindingConfirmationOrder(@Qualifier("confirmationOrderQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_CONFIRMATION_ORDER);
    }

    @Bean
    public Binding bindingTaskOrder(@Qualifier("taskOrderQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_TASK_ORDER);
    }

    @Bean
    public Binding bindingSummaryOrder(@Qualifier("summaryOrderQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_SUMMARY_ORDER);
    }

    @Bean
    public Binding bindingServiceProblem(@Qualifier("serviceProblemQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_SERVICE_PROBLEM);
    }

    // 通知总括队列
    @Bean
    public Queue notifyCommanderQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_NOTIFY_COMMANDER).build();
    }

    // 通知总括绑定
    @Bean
    public Binding bindingNotifyCommander(@Qualifier("notifyCommanderQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_NOTIFY_COMMANDER);
    }


    // 拒绝队列
    @Bean
    public Queue refuseNotifyQueue() {
        return QueueBuilder.durable(RabbitMQConstants.QUEUE_REFUSE_NOTIFY).build();
    }

    // 拒绝绑定
    @Bean
    public Binding bindingRefuseNotify(@Qualifier("refuseNotifyQueue") Queue queue) {
        return BindingBuilder.bind(queue).to(messageExchange()).with(RabbitMQConstants.ROUTING_KEY_REFUSE_NOTIFY);
    }




    /**
     * 配置消息转换器为 JSON
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    /**
     * 配置 RabbitTemplate 使用 JSON 转换器
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(jsonMessageConverter());
        return template;
    }

    /**
     * 配置监听容器工厂使用 JSON 转换器
     */
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
            ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(jsonMessageConverter()); // ✅ 这一行最关键！
        return factory;
    }
}